package com.dcbeida.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dcbeida.bean.Area;
import com.dcbeida.utils.Constants;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class AreaDB {

    private SQLiteDatabase db;
    private static String PATH;//存放数据库地址
    private static final String DBNAME = "beida_addr.db";
    private static final String TABLE_AREA = "db_area";

    public AreaDB(Context mContext) {
        PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + Constants.DEFAULT_DATA_FILE;
        File file = new File(PATH, DBNAME);
        if (!file.exists())
            initData(mContext);


        db = SQLiteDatabase.openOrCreateDatabase(file, null);
    }

    private void initData(Context myContext) {
        try {
            File file = new File(PATH);
            if (!file.exists()) {
                file.mkdirs();
            }

            file = new File(PATH, DBNAME);
            if (!file.exists()) {
                file.createNewFile();
            }

            InputStream inputStream = myContext.getAssets().open("beida_addr.db");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] data = new byte[1024];
            int len;
            if (inputStream != null) {
                while ((len = inputStream.read(data)) != -1) {
                    fileOutputStream.write(data, 0, len);
                }
                fileOutputStream.flush();
                fileOutputStream.close();
                inputStream.close();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public List<Area> getProvinces() {//所有的省份
        List<Area> provinces = new ArrayList<Area>();
        //		SQLiteDatabase db = getWritableDatabase();
        if (db == null) {
            return null;
        }

        Cursor cursor = db.query(TABLE_AREA, null, "pcode = ?", new String[]{"0"}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Area area = new AreaDatabaseBuilder().build(cursor);
                provinces.add(area);
            }
        }

        //		db.close();

        return provinces;
    }

    public Map<String, List<Area>> getAllCities() {//所有城市
        Map<String, List<Area>> allCities = new HashMap<String, List<Area>>();
        //		SQLiteDatabase db = getWritableDatabase();
        if (db == null) {
            return null;
        }

        Cursor cursor = db.query(TABLE_AREA, null, "type = '2'", null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Area area = new AreaDatabaseBuilder().build(cursor);
                List<Area> tempArea = null;
                tempArea = allCities.get(area.getPcode());
                if (tempArea == null) {
                    tempArea = new ArrayList<Area>();
                    allCities.put(area.getPcode(), tempArea);
                }
                tempArea.add(area);
            }
        }

        //		db.close();

        return allCities;
    }

    public Map<String, List<Area>> getAllAreas() {//所有地区
        Map<String, List<Area>> allAreas = new HashMap<String, List<Area>>();
        //		SQLiteDatabase db = getWritableDatabase();
        if (db == null) {
            return null;
        }

        Cursor cursor = db.query(TABLE_AREA, null, "type = '3'", null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Area area = new AreaDatabaseBuilder().build(cursor);
                List<Area> tempArea = null;
                tempArea = allAreas.get(area.getPcode());
                if (tempArea == null) {
                    tempArea = new ArrayList<Area>();
                    allAreas.put(area.getPcode(), tempArea);
                }
                tempArea.add(area);
            }
        }

        //		db.close();

        return allAreas;
    }

    public Area getArea(String code) {//所有地区
        Area area = new Area();
        //		SQLiteDatabase db = getWritableDatabase();
        if (db == null) {
            return null;
        }

        Cursor cursor = db.query(TABLE_AREA, null, "code = ?", new String[]{code}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                area = new AreaDatabaseBuilder().build(cursor);

            }
        }

        //		db.close();

        return area;
    }

    public List<Area> getAreas(String code) {//所有地区
        List<Area> areaList = new ArrayList<Area>();
        //		SQLiteDatabase db = getWritableDatabase();
        if (db == null) {
            return null;
        }

        Cursor cursor = db.query(TABLE_AREA, null, "pcode = ?", new String[]{code}, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Area area = new AreaDatabaseBuilder().build(cursor);
                areaList.add(area);
            }
        }

        //		db.close();

        return areaList;
    }

    public void close() {
        db.close();
    }

}
